<?php

namespace Home\Controller;

use Think\Controller;

class CustomerController extends Controller
{
    var $advert;
    var $product;
    var $category;
    var $gallery;
    var $cart;
    var $address;
    var $orders;
    var $order_product;
    var $customer;

    function __construct()
    {
        parent::__construct();
        $this->advert = M('Advert');
        $this->product = M('Product');
        $this->category = M('Category');
        $this->gallery = M('Gallery');
        $this->cart = M('Cart');
        $this->address = M('Address');
        $this->order = M('Orders');
        $this->order_product = M('Order_product');
        $this->customer = M('Customer');
        $this->customer_id = $_SESSION['customer']['id'];
    }

    /***
     * ajax注册
     */
    public function register()
    {
        $Customer = M('Customer');
        if (IS_AJAX) {
//            p($_POST);exit;
            $pca = explode(" ", I('post.pca'));
            $data['nickname'] = trim(I('post.nickname'));
            $data['customer_on'] = make_on();
            $data['thumb'] = I('post.thumb');
            $data['sex'] = I('post.sex');
            $data['mobile'] = I('post.mobile');
            $data['email'] = I('post.email');
            $data['birthday'] = I('post.birthday');
            $data['province'] = $pca[0];
            $data['city'] = $pca[1];
            $data['area'] = $pca[2];

            $data['token'] = make_token() . time();
            $data['password'] = set_password(I('post.password'));
            $data['created_at'] = time();
            $check_password = I('post.check_password');

            $nickname = $Customer->where("nickname = '$data[nickname]'")->find();
            $mobile = $Customer->where("mobile = '$data[mobile]'")->find();
            $email = $Customer->where("email = '$data[email]'")->find();

            $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
            $telephone = "/^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$/";
            $face = array('status' => 1, 'msg' => '恭喜您，注册成功');
            if ($nickname) {
                $face = array('status' => 0, 'msg' => '用户名已存在');
            }
            if ($mobile) {
                $face = array('status' => 0, 'msg' => '手机号码已存在');
            }
            if ($email) {
                $face = array('status' => 0, 'msg' => '邮箱已存在');
            }
            if (strlen($data['nickname']) < 5 || strlen($data['nickname']) > 20) {
                $face = array('status' => 0, 'msg' => '用户名长度不能小于5位，并且不能大于20位');
            }
            if (!preg_match($pattern, $data['email'])) {
                $face = array('status' => 0, 'msg' => '邮箱格式不正确');
            }
            if (!preg_match($telephone, $data['mobile'])) {
                $face = array('status' => 0, 'msg' => '手机格式不正确');
            }
            if (strlen(I('post.password')) < 6) {
                $face = array('status' => 0, 'msg' => '密码长度不能小于6位');

            }
            if ($check_password != I('post.password')) {
                $face = array('status' => 0, 'msg' => '两次密码输入不一致');

            }
            if ($face['status'] == 1) {
                $Customer->add($data);
            }
            $this->ajaxReturn($face);


        }
        $this->display();
    }

    /***
     * ajax登录
     */
    public function login()
    {
        $Customer = M('Customer');
        if (IS_AJAX) {
            $data['nickname|mobile|email'] = trim(I('post.customername'));
            $data['password'] = set_password(I('password'));
            $customer = $Customer->where($data)->find();
            if (!$customer) {
                $face = array('status' => 0, 'msg' => '该用户或密码错误');
            }

            //登录后跳回之前的页面
            else {
                $_SESSION['customer'] = $customer;
                $face = array('status' => 1, 'msg' => '恭喜您，登录成功');
//                $this->success('登录成功', $_SESSION['url']);
            }
//            else {
//
//
//            }
            $this->ajaxReturn($face);

        }
        $this->display();
    }

    /***
     * 个性签名
     */
    public function editSign()
    {
        if (IS_AJAX) {
            $sign = I('post.sign');
            $result = $this->customer->where("id = '$this->customer_id'")->setField('sign', $sign);
            if ($result) {
                $info = array('status' => 1, 'msg' => '编辑成功');
            } else {
                $info = array('status' => 0, 'msg' => '编辑失败');
            }
            $this->ajaxReturn($info);
        }
    }

    /***
     * 加载会员中心首页
     */
    public function index()
    {
        if (!isset($this->customer_id)) {
            $this->error('您还没有登录，请登录后访问', U('Customer/login'));
        }
        $customerInfo = $this->customer->where("id = '$this->customer_id'")->find();
        $this->assign(compact('customerInfo'));
        $this->display();
    }

    /***
     * 加载订单页面
     */
    public function orders()
    {
        $status = I('get.status');
        if ($status == 'all') {
            $order_map = array('customer_id' => $this->customer_id);
        } else {
            $order_map = array('customer_id' => $this->customer_id, 'a.status' => $status);
        }

        //连表查询 a是orders b是order_product c是product
//        $all_orders = $this->order->alias('a')->field('a.id,a.order_no,a.total_price,a.status as order_status,b.number,b.order_id,b.product_id,c.id as pid,c.name,c.price,c.thumb')
//            ->join('order_product AS b ON b.order_id=a.id', 'LEFT')
//            ->join('product AS c ON c.id=b.product_id', 'LEFT')
//            ->where($order_map)->select();
//        p($all_orders);exit;
        $all_orders = $this->order->alias('a')->where($order_map)->where("customer_id = '$this->customer_id'")->select();
        foreach ($all_orders as &$order) {
            $order['order_product'] = $this->order_product->where("order_id = '$order[id]'")->select();
            foreach ($order['order_product'] as &$order_product) {
                $order_product['content'] = $this->product->where("id = '$order_product[product_id]'")->find();
//                p($order_product['number']);exit;
            }
        }
//        p($order_product['number']);exit;


        $this->assign(compact('all_orders'));
        $this->display();
    }

    /***
     * 修改个人资料
     */
    public function info()
    {
        if (IS_AJAX) {

//            p($_POST);exit;
            $this->customer->create();
            $this->customer->birthday = strtotime(I('post.birthday'));
            $this->customer->where("id = '$this->customer_id'")->save();
            $info = array('status' => 1, 'msg' => '修改成功');
            $this->ajaxReturn($info);
        }
        $customer = $this->customer->where("id = $this->customer_id")->find();
        $this->assign(compact('customer'));
        $this->display();
    }

    /***
     *删除订单
     */
    public function cancel_order()
    {
        $id = I('post.id');
        $orders = $this->order->where("id = '$id'")->find();
        $order_products=$this->order_product->where("order_id = $orders[id]")->select();

//        foreach ($orders as &$order) {
//            $order['order_product'] = $this->order_product->where("order_id = '$order[id]'")->select();
            foreach ($order_products as &$order_product) {
//                $order_product['content'] = $this->product->where("id = '$order_product[product_id]'")->find();
//                if ($order_product['content']['stock'] != '-1') {
//                    $this->product->where("id = $order_product[product_id]")->setInc('stock', $order_product['number']);
//                }
                $this->product->where("id = $order_product[product_id]")->setInc('stock',$order_product['number']);

            }
//        }
        $this->order->where("id = '$id'")->delete();
        $this->order_product->where("order_id = '$id'")->delete();
    }
}